<template>
  <!-- 筛选区 -->
  <div class="sub-filter" v-if="filterData">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a
          href="javasript:;"
          v-for="brand in filterData.brands"
          :key="brand.id"
          @click="changeBrand(brand.id)"
          >{{ brand.name }}</a
        >
      </div>
    </div>
    <div class="item" v-for="p in filterData.saleProperties" :key="p.id">
      <div class="head">{{ p.name }}：</div>
      <div class="body">
        <a
          href="#"
          v-for="attr in p.properties"
          :key="attr.id"
          @click="changeAttrs(attr,p)">{{
          attr.name
        }}</a>
      </div>
    </div>
  </div>
</template>
<script>
import { findSubCategoryFilter } from '@/api/category'
import { useRoute } from 'vue-router'
import { onMounted, ref, inject } from 'vue'
export default {
  name: 'SubFilter',
  setup () {
    const route = useRoute()
    const filterData = ref(null)
    async function loadData () {
      const subId = route.params.id
      const { result } = await findSubCategoryFilter(subId)
      result.brands.unshift({ id: null, name: '全部' })
      result.saleProperties.forEach(p => {
        p.properties.unshift({ id: null, name: '全部' })
      })
      filterData.value = result
    }
    onMounted(() => {
      loadData()
    })

    const setData = inject('setData')
    function changeBrand (id) {
      setData.setBrandId(id)
    }
    function changeAttrs (attr, p) {
      setData.setAttrsData({
        propertyName: attr.name,
        groupName: p.name,
        groupId: p.id
      })
    }
    return { filterData, changeBrand, changeAttrs }
  }
}
</script>
<style scoped lang='scss'>
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    padding: 10px 0;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        &.active,
        &:hover {
          color: $xtxColor;
        }
      }
    }
  }
}
</style>
